package beautiful.butterfly.drds.binlog.connection;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Pattern;

@Slf4j
public class ShardingRule
{
    // 支持分库分表

    // 私有变量
    @Setter
    @Getter
    private Pattern sourceDatabaseNamePattern;
    @Setter
    @Getter
    private Pattern sourceTableNamePattern;
    // 之后的
    @Setter
    @Getter
    private String destinationDatabaseNamePattern;
    @Setter
    @Getter
    private String destinationTableNamePattern;

    // 构造函数
    public ShardingRule(Pattern sourceDatabaseNamePattern, Pattern sourceTableNamePattern, String destinationDatabaseNamePattern, String destinationTableNamePattern)
    {
        this.sourceDatabaseNamePattern = sourceDatabaseNamePattern;
        this.sourceTableNamePattern = sourceTableNamePattern;
        this.destinationDatabaseNamePattern = destinationDatabaseNamePattern;
        this.destinationTableNamePattern = destinationTableNamePattern;
        log.info("regex sharding rule : [" + this.sourceDatabaseNamePattern + "],[" + this.sourceTableNamePattern + "],["
                + this.destinationDatabaseNamePattern + "],[" + this.destinationTableNamePattern + "]");
    }

    public boolean accept(String d, String t)
    {

        return sourceDatabaseNamePattern.matcher(d).matches() && sourceTableNamePattern.matcher(t).matches();
    }

}
